package com.tycmc.signelectroniccontracts.act;

import android.Manifest;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.tycmc.signelectroniccontracts.R;
import com.tycmc.signelectroniccontracts.logic.PreLogin;
import com.tycmc.signelectroniccontracts.tools.base.ActBase;
import com.tycmc.signelectroniccontracts.tools.images.UrlPath;
import com.tycmc.signelectroniccontracts.tools.permissions.PermissionsTool;
import com.tycmc.signelectroniccontracts.tools.store.SharePreName;
import com.tycmc.signelectroniccontracts.ui.PopQianMing;

public class ActLogin extends ActBase implements PreLogin.LoginInterface {

    private PreLogin pre;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_login);
        pre = new PreLogin(this);
        initView();
        SharePreName.clean();

        PermissionsTool.requestPermiss(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, new PermissionsTool.PerMissionCallBack() {
            @Override
            public void permissIsPass(String[] permission, boolean isPass) {
                if (isPass) {
                    UrlPath.init();
                }
            }
        }, this);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        pre.destory();
    }

    private void initView() {
        //获取验证码
        findViewById(R.id.tv_getcode).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pre.getcode(((TextView) findViewById(R.id.et_account)).getText().toString().trim());
            }
        });
        //个人
        findViewById(R.id.tv_next_geren).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                PopQianMing popQianMing = new PopQianMing(ActLogin.this, new PopQianMing.QianMingInterface() {
//                    @Override
//                    public void qianMing(Bitmap bitmap, String path) {
//
//                    }
//                });
//                popQianMing.show(view);
                pre.next(((TextView) findViewById(R.id.et_account)).getText().toString().trim(), ((TextView) findViewById(R.id.et_code)).getText().toString().trim(), true);
            }
        });

    }

    @Override
    public void edCode(int number) {
        TextView teCode = (TextView) findViewById(R.id.tv_getcode);
        if (number > 0) {
            teCode.setText(String.format(getString(R.string.replay_getcode), number + ""));
            teCode.setEnabled(false);
        } else {
            pre.destory();
            teCode.setEnabled(true);
            teCode.setText(R.string.repleycode);
        }
    }
}
